// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
var ParticleSystem = require('ParticleSystem')
cc.Class({
    extends: require('UI-PanelWindow'),

    properties: {
        mask: cc.Node
    },
    setOptions(options) {
        this.options = options;
        this.actionIndex = 0;
        cc.log('setOptions:', options)
        var url = 'plane/' + options.prefab
        var self = this;

        cc.loader.loadRes(url, function (err, res) {
            var node = cc.instantiate(res)
            var bullet = node.getComponent('Boss');
            bullet._destroyImmediate()
            self.mask.addChild(node);
            node.x = 0;
            node.y = 1300;

            self.monster = node;
            self.pathReady = node.getComponent('PathReady')
            let bulletNode = new cc.Node();
            self.mask.addChild(bulletNode)
            self.launcherNode = bulletNode
            self.bossNode = node.getChildByName('launcher')
            self.spanControl = node.getComponentInChildren('SpanControl')
            self.nextAction();
        })
    },
    nextAction() {
        this.bossNode.destroyAllChildren();
        let logic = this.options.status[this.actionIndex]
        if (!logic) {
            // this.actionIndex = 1;
            // logic = this.options.status[0]
            cc.log('运动完成')
            return;
        }

        var path = global.PathConfig[logic.path];
        if (path) {
            this.pathReady.loadData(path);
            this.pathReady.runAction(1)
        } else if (logic.action) {
            this.runCCAction(logic.action)
        }
        if (logic.skill) {
            var r = 0;
            while (logic.skill[r]) {
                this.addLauncher(logic.skill[r])
                r++;
            }
        }
        if (logic.span) {
            this.spanControl.playDBArmature(logic.span)
        }
        this.scheduleOnce(function () {
            if (logic.to != undefined) {
                this.actionIndex = logic.to;
            } else {
                this.actionIndex++
            }
            this.nextAction()
        }.bind(this), logic.time)
        // this.actionIndex++
    },
    addLauncher(skills) {
        for (let i = 0; i < skills.length; i++) {
            let skillbase = global.Common.deepCopy(skills[i]);
            let skillName = skillbase.name
            var particle = global.SkillConfig[skillName]
            if (!particle) {
                cc.warn('没有找到粒子文件')
                continue;
            }
            particle.poolName = skillName
            var nodename = skillName.substring(Math.max(skillName.indexOf('/') + 1, 0));
            let ps = new cc.Node().addComponent(ParticleSystem);
            ps.node.name = nodename
            this.bossNode.addChild(ps.node) //添加一个粒子发射器

            ps.node.rotation = skillbase.r ? skillbase.r : 0;

            if (skillbase['dx']) ps.node.x = skillbase['dx'];
            if (skillbase['dy']) ps.node.y = skillbase['dy'];

            ps.node._skillBase = skillbase

            ps.setParticleLayer(this.launcherNode)
            ps.loadData(particle);
            ps.addEvent(ParticleSystem.Event.CREATE, this.onParticleCreate.bind(this))

            // ps.addEvent(ParticleSystem.Event.CREATE, this.onParticleCreate.bind(this))
            // ps.addEvent(ParticleSystem.Event.REPEAT, this.onParticleRepeat.bind(this))
            // ps.addEvent(ParticleSystem.Event.ALL_FINISH, this.onParticleFinish.bind(this))

            skillbase.delay = skillbase.delay ? skillbase.delay : 0
            skillbase.offset = skillbase.offset ? skillbase.offset : 0
            var time = skillbase.delay + skillbase.offset * Math.random() * (Math.random() < 0.5 ? -1 : 1)
            if (time != 0) {
                ps.enabled = false;
                this.scheduleOnce(function () {
                    ps.enabled = true
                    skillbase.action && self.runSkillAction(skillbase.action, ps.node)
                }, time)
            } else {
                skillbase.action && this.runSkillAction && this.runSkillAction(skillbase.action, ps.node)
            }
        }
    },
    runCCAction(data) {
        var arr = data.split(',')
        var type = arr[0];
        var action = null;
        if (type == 'r') {
            this.monster.getChildByName('body').rotation = arr[2]
            action = cc.rotateTo(arr[1], arr[3]);
            this.monster.getChildByName('body').runAction(action)
        } else if (type == 'mb') {
            action = cc.moveBy(arr[1], cc.v2(arr[2], arr[3]));
            this.monster.runAction(action)
        }else if(type == 'mt'){
            action = cc.moveTo(arr[1],cc.v2(arr[2],arr[3]));
            this.monster.runAction(action)
        }
    },
    onParticleCreate(no) {
        if (no.detail) {
            var skillBase = no.detail.node._source
            if (skillBase.path) {
                var pathReady = no.detail.addComponent('PathReady')
                var path = global.PathConfig[skillBase.path];
                if (path) {
                    no.detail.enabled = false;
                    pathReady.target = no.detail.node;
                    pathReady.loadData(path);
                    pathReady.runAction(1)
                }
            }
        }
    }

});
